package com.sbc.user.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.sbc.common.toolkit.BeanUtil;
import com.sbc.convention.exception.ClientException;
import com.sbc.convention.exception.ServiceException;
import com.sbc.user.dao.entity.TUser;
import com.sbc.user.dao.entity.TUserDeletion;
import com.sbc.user.dao.entity.TUserMail;
import com.sbc.user.dao.entity.TUserPhone;
import com.sbc.user.dao.mapper.TUserDeletionMapper;
import com.sbc.user.dao.mapper.TUserMailMapper;
import com.sbc.user.dao.mapper.TUserMapper;
import com.sbc.user.dao.mapper.TUserPhoneMapper;
import com.sbc.user.pojo.dto.UserInfoDto;
import com.sbc.user.pojo.vo.UserInfoActualRespVo;
import com.sbc.user.pojo.vo.UserBasicInfoReqVo;
import com.sbc.user.pojo.vo.UserContactInfoReqVo;
import com.sbc.user.pojo.vo.UserInfoRespVo;
import com.sbc.user.service.UserInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * @description: 用户信息接口实现
 * @author: songbaicheng
 * @create: 2024-04-12 20:52
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class UserInfoServiceImpl implements UserInfoService {

    private final TUserMapper userMapper;
    private final TUserMailMapper userMailMapper;
    private final TUserPhoneMapper userPhoneMapper;
    private final TUserDeletionMapper userDeletionMapper;

    @Override
    public UserInfoRespVo queryUserInfoByUsername(String username) {

        UserInfoDto userInfo = userMapper.queryUserInfoByUsername(username);

        if (ObjectUtil.isEmpty(userInfo)) {
            throw new ClientException("用户不存在，请检查用户名是否正确!");
        }

        return BeanUtil.convert(userInfo, UserInfoRespVo.class);
    }

    @Override
    public UserInfoActualRespVo queryActualUserInfoByUsername(String username) {

        UserInfoDto userInfo = userMapper.queryUserInfoByUsername(username);

        if (ObjectUtil.isEmpty(userInfo)) {
            throw new ClientException("用户不存在，请检查用户名是否正确!");
        }

        return BeanUtil.convert(userInfo, UserInfoActualRespVo.class);
    }

    @Override
    public UserInfoRespVo queryUserInfoByUserId(String userId) {
        UserInfoDto userInfo = userMapper.queryUserInfoByUserId(userId);

        if (ObjectUtil.isEmpty(userInfo)) {
            throw new ClientException("用户不存在，请检查用户名是否正确!");
        }

        return BeanUtil.convert(userInfo, UserInfoRespVo.class);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateUserBasicInfo(String username, UserBasicInfoReqVo userBasicInfo) {

        LambdaQueryWrapper<TUser> queryWrapper = Wrappers.lambdaQuery(TUser.class)
                .eq(TUser::getUsername, username);
        TUser user = userMapper.selectOne(queryWrapper);
        if (ObjectUtil.isEmpty(user)) {
            throw new ClientException("该用户不存在!");
        }

        LambdaUpdateWrapper<TUser> updateWrapper = Wrappers.lambdaUpdate(TUser.class)
                .eq(TUser::getUsername, username)
                .set(TUser::getRealName, userBasicInfo.getRealName())
                .set(TUser::getIdType, userBasicInfo.getIdType())
                .set(TUser::getIdCard, userBasicInfo.getIdCard());
        int update = userMapper.update(updateWrapper);

        if (!SqlHelper.retBool(update)) {
            throw new ServiceException("更新数据异常!");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateUserContactInfo(String username, UserContactInfoReqVo userContactInfo) {

        LambdaQueryWrapper<TUser> queryWrapper = Wrappers.lambdaQuery(TUser.class)
                .eq(TUser::getUsername, username);
        TUser user = userMapper.selectOne(queryWrapper);
        if (ObjectUtil.isEmpty(user)) {
            throw new ClientException("该用户不存在!");
        }

        LambdaUpdateWrapper<TUser> userUpdateWrapper = Wrappers.lambdaUpdate(TUser.class)
                .eq(TUser::getUsername, username)
                .set(TUser::getPostCode, userContactInfo.getPostCode())
                .set(TUser::getAddress, userContactInfo.getAddress());
        int userUpdate = userMapper.update(userUpdateWrapper);

        LambdaUpdateWrapper<TUserMail> userMailLambdaUpdateWrapper = Wrappers.lambdaUpdate(TUserMail.class)
                .eq(TUserMail::getUsername, username)
                .set(TUserMail::getMail, userContactInfo.getMail());
        int userMailUpdate = userMailMapper.update(userMailLambdaUpdateWrapper);

        LambdaUpdateWrapper<TUserPhone> userPhoneLambdaUpdateWrapper = Wrappers.lambdaUpdate(TUserPhone.class)
                .eq(TUserPhone::getUsername, username)
                .set(TUserPhone::getTelephone, userContactInfo.getTelephone());
        int userPhoneUpdate = userPhoneMapper.update(userPhoneLambdaUpdateWrapper);

        if (!SqlHelper.retBool(userUpdate) && !SqlHelper.retBool(userMailUpdate) && !SqlHelper.retBool(userPhoneUpdate)) {
            throw new ServiceException("更新数据异常!");
        }
    }

    @Override
    public Integer queryUserDeletionNum(Integer idType, String idCard) {
        LambdaQueryWrapper<TUserDeletion> queryWrapper = Wrappers.lambdaQuery(TUserDeletion.class)
                .eq(TUserDeletion::getIdType, idType)
                .eq(TUserDeletion::getIdCard, idCard);
        Long deletionCount = userDeletionMapper.selectCount(queryWrapper);
        return Optional.ofNullable(deletionCount).map(Long::intValue).orElse(0);
    }
}
